package com.shens.blog.utils;

import java.util.List;

import com.shens.blog.service.OnBtnClickListener;
import com.shens.blog.service.RequestCallBack;
import com.shens.blog.ui.JInputPanel;
import com.shens.blog.ui.JOutputPanel;

public class CommUtils {
    private static JOutputPanel moutputPanel;
    public static OnBtnClickListener listener = new OnBtnClickListener() {
        public void onBtnClick(String btnName, String content) {
            switch (btnName.hashCode()) {
                case -1422541317:
                    if (btnName.equals("addBtn")) {
                        CommUtils.addUrl(content);
                    }
                    break;
                case -1315418018:
                    if (btnName.equals("exitBtn")) {
                        RequestUtil.exitCurrentThread();
                        CommUtils.moutputPanel.appentOutPrint("中止请求 exit。");
                    }
                    break;
                case 198267974:
                    if (btnName.equals("actionBtn")) {
                        CommUtils.requestArticleUrl();
                    }
            }

        }
    };

    public CommUtils() {
    }

    public static void addListener(JInputPanel inputPanel, JOutputPanel outputPanel) {
        moutputPanel = outputPanel;
        outputPanel.setOnBtnClickListener(listener);
        inputPanel.setOnBtnClickListener(listener);
    }

    private static void addUrl(String content) {
        if (content.matches("[a-zA-z]+://[^\\s]*")) {
            moutputPanel.appArticleUrlList(content);
        } else {
            moutputPanel.appentOutPrint("請輸入一个文章地址");
        }

    }

    private static void requestArticleUrl() {
        List<String> urls = moutputPanel.getArticleUrlList();
        RequestUtil.requestData(urls, new RequestCallBack() {
            public void requesCallBack(String info) {
                CommUtils.moutputPanel.appentOutPrint(info);
            }
        });
    }
}
